package com.lxyk.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.lxyk.mapper.SalaryMapper;
import com.lxyk.pojo.Salary;
import com.lxyk.service.SalaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SalaryServiceImpl implements SalaryService {
    @Autowired
    private SalaryMapper salaryMapper;
    @Override
    public PageInfo<Salary> calcSalaryByCondition(String startDate, String endDate, Integer pageNum, Integer pageSize) {
        //当前时间为结束时间
        if (StrUtil.isEmpty(endDate)){
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            endDate = sdf.format(date);
        }
        List<Salary> salaryList = salaryMapper.calcSalaryByCondition(startDate, endDate);
        //工资降序
        Collections.sort(salaryList,(o1, o2) -> {
            if (o1.getFinalSalary() > o2.getFinalSalary()){
                return 1;
            }else if (o1.getFinalSalary() < o2.getFinalSalary()){
                return -1;
            }else {
                return 0;
            }
        });


        return new PageInfo<>(salaryList);
    }

    @Override
    public PageInfo<Salary> IsNullWorker(Integer pageNum, Integer pageSize) {
        List<Salary> salaryList = salaryMapper.IsNullWorker();
        return new PageInfo<>(salaryList);
    }


}
